/*
Package make
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2024/4/15 17:38
@File     : make_controller.go
@Project  : gohub
@Desc     :
*/
package make

import (
	"fmt"
	"github.com/spf13/cobra"
	"gohub/pkg/console"
	"strings"
)

var CmdMakeAPIController = &cobra.Command{
	Use:   "apicontroller",
	Short: "Create a new API controller, example:make apicontroller v1/user",
	Run:   runMakeAPIController,
	Args:  cobra.ExactArgs(1), // 1 argument is required
}

func runMakeAPIController(cmd *cobra.Command, args []string) {
	//处理参数，要求附带API版本号，例如v1或v2
	array := strings.Split(args[0], "/")
	if len(array) != 2 {
		console.Exit("api controller name format: v1/user")
	}
	//apiVersion用来拼接目标路径
	//name用来生成cmd.Model实例
	apiVersion, name := array[0], array[1]
	model := makeModelFormString(name)

	//组建目标路径
	filePath := fmt.Sprintf("app/http/controllers/api/%s/%s_controller.go", apiVersion, model.TableName)

	//基于模板创建文件（做好变量替换）
	createFileFromStub(filePath, "apicontroller", model)
}
